package com.guli.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    //使用递归的方法  （三级分类树优化）
    @Override
    public List<CategoryEntity> treeList() {
        //1.查询所有的三级分类信息 1425条
        List<CategoryEntity> allList = baseMapper.selectList(null);

        //2.在allLis中获取一级分类集合
        //stream()变成流的形式
        //item 就是集合中的每一个元素
        List<CategoryEntity> children = allList.stream().filter(item -> {
            //拿到条件为0的1级分类
            return item.getParentCid().equals(0L);
        }).map(item -> {//item 此时为过滤后的结果
            //需要在这个位置找到二级分类
            //找子元素的方法 children为二级分类  （已经包含了三级分类）
            List<CategoryEntity> children1 = getChildren(allList, item);
            item.setChildren(children1);

            return item;
        }).sorted((s1,s2)->{
            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
        }).collect(Collectors.toList());
        return children;
    }
    //找子元素的方法
    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity categoryEntity){
        List<CategoryEntity> children = allList.stream().filter(item -> {
            //如果分类的父id与它的id相同则为二级分类
            return item.getParentCid().equals(categoryEntity.getCatId());
        }).map(item -> {
            //查询三级分类  类推
            List<CategoryEntity> children1 = getChildren(allList, item);
            item.setChildren(children1);
            return item;
        }).sorted((s1,s2)->{
            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
        }).collect(Collectors.toList());
        return children;
    }



    /**
    //三级树优化后
    //递归
    @Override
    public List<CategoryEntity> treeList() {
        //查询所有三级分类
        List<CategoryEntity> allList = baseMapper.selectList(null);

        //在allList中获取一级分类集合
        //item就是集合中的每个元素
        List<CategoryEntity> l1List = allList.stream().filter(item -> {

            return item.getParentCid().equals(01);
        }).map(item -> {
            //item就是过滤之后的结果
            //需要在这个位置找二级分类
            //找子元素的方法 children 二级分类
            List<CategoryEntity> children = getChildren(allList,item);
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());

        return l1List;
    }

    //递归调用
    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity categoryEntity) {
        List<CategoryEntity> children = allList.stream().filter(item ->{
            return item.getParentCid().equals(categoryEntity.getCatId());
        }).map(item ->{
            List<CategoryEntity> children1 = getChildren(allList,item);
            item.setChildren(children1);
            return item;
        }).collect(Collectors.toList());

        return children;
    }


*/


    //三级树优化前
//    @Override
//    public List<CategoryEntity> treeList() {
//        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
//        w1.eq("parent_cid",0);
//        List<CategoryEntity> l1list = baseMapper.selectList(w1);
//
//        for (CategoryEntity l1 : l1list) {
//            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
//            w2.eq("parent_cid",l1.getCatId());
//            List<CategoryEntity> l2list = baseMapper.selectList(w2);
//            l1.setChildren(l2list);
//            for (CategoryEntity l2 : l2list) {
//                List<CategoryEntity> l3list = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",l2.getCatId()));
//                l2.setChildren(l3list);
//            }
//        }
//        return l1list;
//    }

}